package com.gxa.community.sso.controller;

import com.aliyuncs.exceptions.ClientException;
import com.gxa.community.core.common.CommonVar;
import com.gxa.community.core.common.RegExpression;
import com.gxa.community.core.common.ResultMsg;
import com.gxa.community.core.utils.R;
import com.gxa.community.sso.service.LoginService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

/**
 * 作者： Juran on 2022-07-24 14:34
 * 作者博客：iit.la
 */
@RestController
@RequestMapping("user")
public class LoginController {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    LoginService loginService;

    @ApiOperation("登录接口")
    @PostMapping("login")
    public R login(@Param("phone") String phone,
                   @Param("code") String code) {
        //判断手机号格式是否正确
        if (!Pattern.matches(RegExpression.PHONE_REG.getExpression(), phone) || ObjectUtils.isEmpty(code))
            return R.error(ResultMsg.SMS_PHONE_FORMAT_ERROR.getMsg());

        //进行登录
        return loginService.login(phone, code);
    }

    @ApiOperation("请求短信接口")
    @RequestMapping("/sendSms/{token}/{phone}")
    public R sendSms(@PathVariable("token") String token,
                     @PathVariable("phone") String phone) throws ClientException {

        //判断收好和token是否为空
        if (ObjectUtils.isEmpty(token) &&
                ObjectUtils.isEmpty(phone) &&
                (phone.length() != 11))
            return R.error(ResultMsg.REQUEST_PARAMS_ERROR.getMsg());

        //判断手机号格式是否正确
        if (!Pattern.matches(RegExpression.PHONE_REG.getExpression(), phone))
            return R.error(ResultMsg.SMS_PHONE_FORMAT_ERROR.getMsg());

        return loginService.sendSms(token, phone);
    }

    @ApiOperation("获取短信接口")
    @PostMapping("/SendMsg")
    public R getSendSmsInterface(String phone) {
        System.out.println(phone);
        //判断手机号格式是否正确。
        if (!Pattern.matches(RegExpression.PHONE_REG.getExpression(), phone))
            return R.error(ResultMsg.SMS_PHONE_FORMAT_ERROR.getMsg());

        //手机号是否被限制
        if (!ObjectUtils.isEmpty(redisTemplate.opsForValue().get(CommonVar.PHONE_SEND_MSG_MARK.getKeyWord() + phone)))
            return R.error(ResultMsg.SMS_PHONE_REQUEST_TIME.getMsg());

        if (!ObjectUtils.isEmpty(redisTemplate.opsForValue().get(CommonVar.SEND_SMS_INTERFACE_GET.getKeyWord() + phone)))
            return R.error(ResultMsg.SMS_PHONE_REQUEST_TIME.getMsg());

        return loginService.getSendSmsInterface(phone);
    }

    @PostMapping("checkWithToken")
    public R checkWithToken(String token) {
        return loginService.checkWithToken(token);
    }

    @PostMapping("/loginOut")
    public R loginOut(String token, HttpServletResponse response) {

        redisTemplate.delete(CommonVar.USER_LOGIN_AXLAB_TOKEN.getKeyWord() + token);

        redisTemplate.delete(CommonVar.AGENT_LOGIN_AXLAB_TOKEN.getKeyWord() + token);

        response.setHeader("token", "");

        return R.ok("退出成功");
    }

}
